//
//  EmojiManage.swift
//  EmojiKeyBoard
//
//  Created by macs on 15/11/20.
//  Copyright © 2015年 BlackTail. All rights reserved.
//

import UIKit

class EmojiManage: NSObject {
    
    //添加单例化方法
    static let shareManage : EmojiManage = EmojiManage()
    
    var packages = [PackagesModel]()
    override init() {
        super.init()
        loadEmoji()
    }
    
    //
    func getAttributeText (str : String, font : UIFont) -> NSAttributedString {
        
        let pattrn = "\\[.*?\\]"
        //忽略大小写.忽略换行 [.DotMatchesLineSeparators,.IgnoreMetacharacters]
        let regular = try! NSRegularExpression(pattern: pattrn, options: [] )
        let result = regular.matchesInString(str, options: [], range: NSRange(location: 0,length: str.characters.count))
        
        var  count = result.count - 1
        
        //创建可变属性文本
        let strMM = NSMutableAttributedString(string: str)
        
        //反向循环遍历数组,取出字符串
        while count >= 0 {
            //取出每个数据的 range
            let range = result[count].rangeAtIndex(0)
            let subString = (str as NSString).substringWithRange(range)
            //print(subString)
            
            //根据文字获取到表情图片
            if let emoji = getEmoji(subString) {
                
                //根据图片获取到属性文本
                let imageText = EmojiAttachement().emojiGetImageText(emoji, font : font)
                //将表情字符串中属性文本中文字替换为图片
                strMM.replaceCharactersInRange(range, withAttributedString: imageText)
            }
            
            count--
        }
        
       return strMM
    }
    
    //在表情数组中寻找表情字符
    func getEmoji (string : String) ->Emoji?{
        
        //遍历所有的表情图片数组
        for p in EmojiManage().packages {
            //filter数组过滤器
            let list = p.emoticons.filter({ (em) -> Bool in
                //如果元素的名称与传入字符串相等 上面的filter返回一个[emoji]这样的数组
                return em.chs == string
            })
            
            if let e = list.last{
                return e
            }
            
        }
        
        return nil
    }
    
    func loadEmoji(){
        
        let path = NSBundle.mainBundle().pathForResource("emoticons.plist", ofType: nil, inDirectory: "Emoticons.bundle")
        //获取总的字典
        let dict = NSDictionary(contentsOfFile: path!)
        
        guard let packages = dict!["packages"] as? [[String : AnyObject]] else {
            
            return
        }
        for item in packages {
            
            let id = item["id"] as! String
            //print(id)
            loadGroup(id)
        }
    }
    //
    func loadGroup(id : String) {
        let path  = NSBundle.mainBundle().pathForResource("Info.plist", ofType: nil, inDirectory: "Emoticons.bundle/\(id)")
        let dict = NSDictionary(contentsOfFile: path!)
        
        let name = dict!["group_name_cn"] as! String
        let emoticons = dict!["emoticons"] as! [[String : String]]
        //字典转为模型
        let p = PackagesModel(id: id, name: name, emoticonArray: emoticons)
        
        packages.append(p)
        
    }
    
    
}
